﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using DigiLib;

public partial class Controls_admin_ModuleByCates : Admin_Controls_Base {
    protected void Page_Load(object sender, EventArgs e) {
        base.removeHGC(divMessage);
        if (!IsPostBack) {
            try {
                loadCategories();
                ddlCategories.SelectedValue = Category_ID.ToString();
                loadModule();
            } catch (Exception ex) { divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message); }
        }
    }
    private DataTable loadSource(int c, DataTable objDT, int l, string name) {
        DataTable dtTop;
        dtTop = BO_portal_modules.SelectAdmin(c, l, string.Empty);
        if (dtTop != null && dtTop.Rows.Count > 0) {
            string tip = String.Empty;
            string m = String.Empty;
            foreach (DataRow drow in dtTop.Rows) {
                m += drow["MODULE_ID"].ToString() + ";";
                tip += drow["FULL_NAME"].ToString() + ",";
            }
            tip = tip.Substring(0, tip.Length - 1);
            DataRow dr = objDT.NewRow();
            dr["LOCATION_ID"] = l;
            dr["LOCATION"] = name;
            dr["COUNT"] = dtTop.Rows.Count;
            dr["TIP"] = tip;
            dr["MODULE_ID"] = m;
            objDT.Rows.Add(dr);
        }
        return objDT;
    }
    protected void loadModule() {
        try {
            int c = Convert.ToInt32(ddlCategories.SelectedValue);
            title.InnerText = "Modules Theo Vị Trí";
            DataTable objDT = new DataTable();
            objDT.Columns.Add("LOCATION_ID");
            objDT.Columns.Add("LOCATION");
            objDT.Columns.Add("COUNT");
            objDT.Columns.Add("TIP");
            objDT.Columns.Add("MODULE_ID");
            ObjDT = BO_portal_locations.SelectAll(1);
            foreach (DataRow drow in ObjDT.Rows) {
                objDT = loadSource(c, objDT, Convert.ToInt32(drow["LOCATION_ID"]), drow["LOCATION_NAME"].ToString());
            }
            base.bindRepeater(rptModule, objDT, divMessage);
        } catch (Exception ex) {
            divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
    }
    protected void rptModule_ItemCommand(object source, RepeaterCommandEventArgs e) {
        try {
            int location_id = Convert.ToInt32(e.CommandName);
            string module_idList = e.CommandArgument.ToString();
            BO_portal_modules.Delete(module_idList, ";", 1);
            loadModule();
        } catch (Exception ex) {
            divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
    }
    protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e) {
        loadModule();
    }
    private void loadCategories() {
        ddlCategories.Items.Clear();
        base.bindDDL(ddlCategories, BO_portal_categories.SelectAll(), "FULL_CATEGORY_NAME", "CATEGORY_ID");
    }
}